package com.cjl.dental_clinic.base;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.bind.annotation.*;

/**
 * @author cjl
 * @create 2022-08-04 8:57 AM
 **/

public abstract class BaseController<T extends BaseEntity, S extends IService<T>> {
    protected final S baseService;

    public BaseController(S baseService) {
        this.baseService = baseService;
    }

    /**
     *  查询详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResVo<T> detail(@PathVariable("id") Integer id) {
        return ResVo.<T>ok(this.baseService.getById(id));
    }

    /**
     * 删除单个
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResVo<Boolean> remove(@PathVariable("id") Integer id) {
        return ResVo.<Boolean>ok(this.baseService.removeById(id));
    }

    /**
     * 更新
     * @param entity
     * @return
     */
    @PutMapping("update")
    public ResVo<Boolean> update(T entity) {
        T existEntity = this.baseService.getById(entity.getId());
        if (ObjectUtil.isNull(existEntity)) {
            return ResVo.error(401, "修改的数据不存在");
        }
        return ResVo.<Boolean>ok(this.baseService.updateById(entity));
    }

    /**
     * 添加
     * @param entity
     * @return
     */
    @PostMapping("save")
    public ResVo<Boolean> save(T entity) {
        return ResVo.<Boolean>ok(this.baseService.save(entity));
    }

}
